<template>
  <div class="login">
    <div class="logo">
      <img src="@/assets/img/logo.png" alt="my login image" />
    </div>
    <!-- 手机号 -->
    <div class="text_group">
      <div :class="{input_group:true,'is-invalid':phoneId_invalid}">
        <input type="input" placeholder="手机号" v-model="phoneCode" />
        <button @click="getCode" class="btn" v-show="show">获取验证码</button>
        <button v-show="!show" class="count">已发送({{count}}s)</button>
      </div>
      <div v-if="phoneId_invalid" class="invalid-feedback">请输入正确的手机号</div>
    </div>
    <!-- 验证码 -->
    <div class="text_group">
      <div :class="{input_group:true,'is-invalid':code_invalid}">
        <input type="input" placeholder="验证码" v-model="code" />
      </div>
      <div v-if="code_invalid" class="invalid-feedback">验证码输入有误</div>
      <div style="color:red" v-if="over_invalid">验证码已过期 请重新获取</div>
    </div>
    <div class="login_des">
      <p>
        新用户登录即自动注册，并表示已同意
        <span>《用户服务协议》</span>和
        <span>《隐私权政策》</span>
      </p>
    </div>
    <!-- 登录按钮 -->
    <div class="login_btn">
      <button @click="login">登录</button>
    </div>
    <div class="login_about">
      <a>关于我们</a>
    </div>
  </div>
</template>

<script>
import { log } from "util";
export default {
  data() {
    return {
      phoneId_invalid: false,
      code_invalid: false,
      over_invalid: false,
      phoneCode: "",
      code: "",
      isShow: true,
      show: true,
      count: "",
      timer: null,
    };
  },
  methods: {
    //获取验证码
    async getCode() {
      this.over_invalid = false; //初始化验证码过期的问题
      //当手机号码的长度小于1,让隐藏的元素显示出来 使用正则进行验证
      const reg = /^[1][3-9][0-9]{9}$/;
      if (!reg.test(this.phoneCode)) {
        this.phoneId_invalid = true;
      } else {
        this.phoneId_invalid = false;
        const { data } = await this.$axios.post("/ele/sendCode", {
          phoneCode: this.phoneCode,
        });
        if (data.ok === 1) {
          this.code = data.code;
          //发送验证码倒计时
          const TIME_COUNT = 30;
          if (!this.timer) {
            this.count = TIME_COUNT;
            this.show = false;
            this.timer = setInterval(() => {
              if (this.count > 0 && this.count <= TIME_COUNT) {
                this.count--;
              } else {
                this.show = true;
                clearInterval(this.timer);
                this.timer = null;
              }
            }, 1000);
          }
        }
      }
    },
    //进行登陆
    async login() {
      const { data } = await this.$axios.post("/ele/login", {
        phoneCode: this.phoneCode,
        code: this.code,
      });
      this.code_invalid = true;
      console.log(222, data);
      if (data.ok === 2) {
        this.over_invalid = true;
        this.code_invalid = false;
      }
      if (data.ok === 1) {
        //登陆成功
        localStorage.token = data.token;
        localStorage.phoneCode = this.phoneCode;
        this.$router.push("/my");
      }
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  width: 100%;
  height: 100%;
  padding: 30px;
  box-sizing: border-box;
  background: #fff;
}
.logo {
  text-align: center;
}
.logo img {
  width: 150px;
}
.text_group,
.login_des,
.login_btn {
  margin-top: 20px;
}
.btn {
  color: #ccc;
}
.login_des {
  color: #aaa;
  line-height: 22px;
}
.login_des span {
  color: #4d90fe;
}
.login_btn button {
  width: 100%;
  height: 40px;
  background-color: #02b6fd;
  border-radius: 24px;
  color: white;
  font-size: 14px;
  border: none;
  outline: none;
}
.login_about {
  margin-top: 30px;
  text-align: center;
  color: #aaa;
  font-size: 12px;
}
.login_btn button[disabled] {
  background-color: #8bda81;
}

.input_group {
  border: 1px solid #ccc;
  border-radius: 4px;
  padding: 10px;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}
.input_group input {
  height: 100%;
  width: 60%;
  border: none;
  outline: none;
}
.input_group button {
  border: none;
  outline: none;
  background: #fff;
}
.input_group button[disabled] {
  color: #aaa;
}
.is-invalid {
  border: 1px solid red;
}
.invalid-feedback {
  color: red;
  padding-top: 5px;
}
.count {
  color: #2395ff;
}
</style>
